package org.wanho.servlet;

import com.alibaba.fastjson.JSON;
import org.wanho.entity.Account;
import org.wanho.util.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：汤小洋
 * Date：2023-05-04 10:21
 * Description：<描述>
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /**
         * 通过CORS允许跨域访问
         */
        resp.setHeader("Access-Control-Allow-Origin","*"); // 允许任何域的跨域请求

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        ResponseResult result;
        if("admin".equals(username) && "123".equals(password)){
            Account account = new Account(101,"admin","123","周易","1383838438");
            result = ResponseResult.success(account);
        }else{
            result = ResponseResult.fail("登录失败！");
        }

        resp.setContentType("application/json;charset=utf8");
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.close();
    }
}
